#! /usr/bin/env python
#coding=utf-8
from uliweb import expose
from sqlalchemy import *
from uliweb.orm import *
from uliweb.utils.pyini import Ini
from sqlalchemy.sql import select
from sqlalchemy.sql import and_, or_, not_
import time, sys
sys.path.insert(0, '../apps')

from departments.models import *
from myuser.models import *

from systems.models import *
from requirements.models import *
from assignments.models import *

from charts.models import *
from tasks.models import *

import datetime
from datetime import date
from get_month_quarter import *

db = get_db_connection()
def get_assignment_report_detail(month_first_day,month_last_day,report_type):
    
    #从settings.ini获取任务类型编号
    #settings = Ini('../apps/settings.ini')

    req_ass = get_model('assignment')
    req_retail = get_model('assignment_report_detail')
    req_t_detail = get_model('tasks_develop_report_detail')
    #获取本周末日期

#    today = datetime.date.today()
#    today = month_last_day
    
    #first_day,last_day = get_week()
    last_day = month_last_day
    first_day = month_first_day
    report_type = report_type

    obj_ass =  req_ass.filter(or_(req_ass.c.stage != 'FN',
                                and_(req_ass.c.finish_date != None,
                                     req_ass.c.finish_date >= first_day,
                                     req_ass.c.stage == 'FN')))              
#    res_ass_rs = res_ass.execute()
    #print 'obj_ass',obj_ass
    data={}
    
    #max_amount = 0
#    cut_ass_id = ''
    
    for row in obj_ass:
        y = row.ass_id.split('_',1)[0]

#        print data.has_key(y)
        if not data.has_key(y):
            data[y] = {}
        if not data[y].has_key('ass'):
            data[y]['max_amount'] = row.amount_inner + row.amount_outer           
            #         data[y]['cut_ass_id'] = y
            data[y]['ass'] = row.id
            data[y]['ass_id'] = row.ass_id
            data[y]['name'] = row.name
            data[y]['kfzx'] = row._kfzx_
            #      data[y]['tx'] = tx
            data[y]['type'] = row.type
            data[y]['stage'] = row.stage
            data[y]['release_date'] = row.release_date
            data[y]['begin_date'] = row.begin_date
            data[y]['end_date'] = row.end_date
            if row.stage == '16':#阶段为决算
                data[y]['js_date'] = row.acceptance_finish_date
                data[y]['t_js'] = 1
            else:
                data[y]['js_date'] = None
                data[y]['t_js'] = 0
            if row.stage == 'FN':#阶段为完成
                data[y]['finish_date'] = row.finish_date
                data[y]['t_finish'] = 1
            else:
                data[y]['finish_date'] = None
                data[y]['t_finish'] = 0
            data[y]['status'] = row.status
            data[y]['delay_classes'] = row.delay_classes
            data[y]['delay_reason'] = row.delay_reason
            data[y]['advanced_if'] = row.advanced_if
            data[y]['risk_if'] = row.risk_if
            data[y]['risk_reason'] = row.risk_reason
            #      data[y]['systems'] = systems
            if row.stage != 'FN' and row.stage != '16' and row.status != '4':#判断阶段为完成或决算，则任务书算完成
                data[y]['t_implement'] = 1
            else:
                data[y]['t_implement'] = 0
            if row.stage == '15':#阶段为验收
                data[y]['t_ys'] = 1
            else:
                data[y]['t_ys'] = 0
            if row.end_date <= last_day and row.stage != 'FN' and row.stage != '16' and row.status != '4' and row.status != '5':
                data[y]['t_delay'] = 1
            else:
                data[y]['t_delay'] = 0
            if row.status == '4' or row.status == '5':#任务书状态为暂停或终止
                data[y]['t_status'] = 1
            else:
                data[y]['t_status'] = 0
            for tx_row in row.tx.ids():            
                data[y]['tx'] = tx_row
                break
            for tx_row in row.systems.ids():            
                data[y]['system'] = tx_row
                break
            
            data[y]['report_date'] = last_day
            data[y]['report_type'] = report_type
            data[y]['t_task_delay'] = 0
            data[y]['task_delay'] = ''
                        
#            data[y]['cut_ass_id'] = row.ass_id.split('_',1)[0]
        else:
#            print 'ceshi'
            if data[y]['max_amount'] < row.amount_inner + row.amount_outer:
                data[y]['max_amount'] = row.amount_inner + row.amount_outer            
#                data[y]['cut_ass_id'] = y
                data[y]['ass'] = row.id
                data[y]['ass_id'] = row.ass_id
                data[y]['name'] = row.name
                data[y]['kfzx'] = row._kfzx_
        #        data[y]['tx'] = tx
                data[y]['type'] = row.type
                data[y]['stage'] = row.stage
                data[y]['release_date'] = row.release_date
                data[y]['begin_date'] = row.begin_date
                data[y]['end_date'] = row.end_date
                if row.stage == '16':#阶段为决算
                    data[y]['js_date'] = row.acceptance_finish_date
                    data[y]['t_js'] = 1
                else:
                    data[y]['js_date'] = None
                    data[y]['t_js'] = 0
                if row.stage == 'FN':#阶段为完成
                    data[y]['finish_date'] = row.finish_date
                    data[y]['t_finish'] = 1
                else:
                    data[y]['finish_date'] = None
                    data[y]['t_finish'] = 0
                data[y]['status'] = row.status
                data[y]['delay_classes'] = row.delay_classes
                data[y]['delay_reason'] = row.delay_reason
                data[y]['advanced_if'] = row.advanced_if
                data[y]['risk_if'] = row.risk_if
                data[y]['risk_reason'] = row.risk_reason
        #        data[y]['systems'] = systems
                if row.stage != 'FN' and row.stage != '16' and row.status != '4':#判断阶段为完成或决算，则任务书算完成
                    data[y]['t_implement'] = 1
                else:
                    data[y]['t_implement'] = 0
                if row.stage == '15':#阶段为验收
                    data[y]['t_ys'] = 1
                else:
                    data[y]['t_ys'] = 0
                if row.end_date <= last_day and row.stage != 'FN' and row.stage != '16' and row.status != '4' and row.status != '5':
                   data[y]['t_delay'] = 1
                else:
                   data[y]['t_delay'] = 0
                if row.status == '4' or row.status == '5':#任务书状态为暂停或终止
                    data[y]['t_status'] = 1
                else:
                    data[y]['t_status'] = 0
                for tx_row in row.tx.ids():            
                    data[y]['tx'] = tx_row
                    break
                for tx_row in row.systems.ids():            
                    data[y]['system'] = tx_row
                    break
                
                data[y]['report_date'] = last_day
                data[y]['report_type'] = report_type
#        print '---', row.id, row.stage, data[y]['t_finish']
        
#    print data            
    task_delay = ''
    for y,t in data.items():
        cut_ass_id = t['ass_id'].split('_',1)[0]
        task_delay = select([func.distinct(req_t_detail.c.task_id)],
                             and_(req_t_detail.c.assignment.in_(select([req_ass.c.id],
#-------------------提交生产前需要修改---------------------------#
                                                                and_(func.substr(req_ass.c.ass_id,1,func.instr(req_ass.c.ass_id,'_')-1) == cut_ass_id))),
#                                                                and_(func.substr(req_ass.c.ass_id,1,9) == cut_ass_id))),
                                  req_t_detail.c.t_delay == 1,
                                  req_t_detail.c.report_type == report_type,
                                  req_t_detail.c.report_date == last_day)
                                ).group_by(req_t_detail.c.task_id)
                                
        rs_task_delay = task_delay.execute()
        for row1 in rs_task_delay:
            task_id, = row1
            data[y]['t_task_delay'] = 1
            data[y]['task_delay'] = data[y]['task_delay'] + task_id + '; \n'
    #更新任务书延迟状态
#    for y,t in data.items():
#        if data[y]['t_task_delay'] == 1:
#            if row.assignment.status != '2' and :
#                req_ass.table.update(req_ass.c.id ==row.assignment.id).execute(status = '2')
#        else:
#            if row.assignment.status == '2':
#                req_ass.table.update(req_ass.c.id ==row.assignment.id).execute(status = '1')
#                
#        data[y].pop('max_amount',None)
#    print 'b',data 
#
#    print data
    return data

